<?php

class Core_Agent_Component_Password_Controller_ChangePassword extends Dcmp_Component_Controller {

    public function indexAction(){
        Kd_Web::getPage()->setTitle('修改密码');
        Kd_Web::getPage()->getNavigation()->addLabel('修改密码');
        return $this->viewResult();
    }

    public function changePasswordPostAction(){
        $data = $this->getRequest()->getForm('content');
        $oldPassword = $data['oldpassword'];
        $model = App::getModel('permission/account')->loadByUsername($this->getIdentity()->getName());
        if(strcasecmp($data['validate_code'],Kd_Web::getSession()->get('validate_code'))!=0){
            $this->getMessager()->addError($this->__('agent.label.error_validateimg'));
            return  $this->redirectResult($this->getUrl(NULL,'index'));
        }
        if(md5($oldPassword) != $model->getPassword()){
            $this->getMessager()->addError($this->__('agent.label.error_password'));
            return  $this->redirectResult($this->getUrl(NULL,'index'));
        }
        $model->changePassword($model->getUserName(),$data['newpassword']);
        $this->getMessager()->addInform($this->__('agent.label.change_success'));
        return  $this->redirectResult($this->getUrl(NULL,'index','index','index'));
    }
}